package com.ruoyi.web.controller.fantasy;

import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fantasy.service.IFantasyCitiesService;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;

/**
 * 城市 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-05
 */
@Controller
@RequestMapping("/fantasy/fantasyCities")
public class FantasyCitiesController extends BaseController
{
    private String prefix = "fantasy/fantasyCities";
	
	@Autowired
	private IFantasyCitiesService fantasyCitiesService;
	
	@RequiresPermissions("fantasy:fantasyCities:view")
	@GetMapping()
	public String fantasyCities()
	{
	    return prefix + "/fantasyCities";
	}
	
	/**
	 * 查询城市列表
	 */
	@RequiresPermissions("fantasy:fantasyCities:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyCity fantasyCities)
	{
		startPage();
        List<FantasyCity> list = fantasyCitiesService.selectFantasyCitiesList(fantasyCities);
		fantasyCitiesService.transforData(list);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出城市列表
	 */
	@RequiresPermissions("fantasy:fantasyCities:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyCity fantasyCities)
    {
    	List<FantasyCity> list = fantasyCitiesService.selectFantasyCitiesList(fantasyCities);
        ExcelUtil<FantasyCity> util = new ExcelUtil<FantasyCity>(FantasyCity.class);
        return util.exportExcel(list, "fantasyCities");
    }
	
	/**
	 * 新增城市
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存城市
	 */
	@RequiresPermissions("fantasy:fantasyCities:add")
	@Log(title = "城市", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyCity fantasyCities)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyCities.setCreateBy(user.getUserId().toString());
		fantasyCities.setCreateTime(new Date());
		fantasyCities.setUpdateBy(user.getUserId().toString());
		fantasyCities.setUpdateTime(new Date());
		return toAjax(fantasyCitiesService.insertFantasyCities(fantasyCities));
	}

	/**
	 * 修改城市
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		FantasyCity fantasyCities = fantasyCitiesService.selectFantasyCitiesById(id);
		mmap.put("fantasyCities", fantasyCities);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存城市
	 */
	@RequiresPermissions("fantasy:fantasyCities:edit")
	@Log(title = "城市", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyCity fantasyCities)
	{
		SysUser user = ShiroUtils.getSysUser();
		fantasyCities.setUpdateBy(user.getUserId().toString());
		fantasyCities.setUpdateTime(new Date());
		return toAjax(fantasyCitiesService.updateFantasyCities(fantasyCities));
	}
	
	/**
	 * 删除城市
	 */
	@RequiresPermissions("fantasy:fantasyCities:remove")
	@Log(title = "城市", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyCitiesService.deleteFantasyCitiesByIds(ids));
	}

	/**
	 * 禁用城市
	 */
	@RequiresPermissions("fantasy:fantasyCities:disable")
	@Log(title = "城市", businessType = BusinessType.DISABLE)
	@PostMapping( "/disable")
	@ResponseBody
	public AjaxResult disable(String ids)
	{
		return toAjax(fantasyCitiesService.disableFantasyCitiesByIds(ids));
	}

	/**
	 * 还原城市
	 */
	@RequiresPermissions("fantasy:fantasyCities:disable")
	@Log(title = "城市", businessType = BusinessType.RESTORE)
	@PostMapping( "/restore")
	@ResponseBody
	public AjaxResult restore(String ids)
	{
		return toAjax(fantasyCitiesService.restoreFantasyCitiesByIds(ids));
	}
}
